<?php
//require section
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_tickets.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_tickets.php';
$titfle = 'Admin Page';



function add($number) {
    $id_save = "";
    if (!empty($_POST['hidd_id'.$number])) {
        $id_save = $_POST['hidd_id'.$number];
    }
    $name_save = "";
    if (!empty($_POST['txt_name'.$number])) {
        $name_save = $_POST['txt_name'.$number];
    }
    $show_date_save = "";
    if (!empty($_POST['dtp_show_date'.$number])) {
        $show_date_save = $_POST['dtp_show_date'.$number];
    }
    $time_start_save = "";
    if (!empty($_POST['dtp_time_start'.$number])) {
        $time_start_save = $_POST['dtp_time_start'.$number];
    }
    $duration_save = "";
    if (!empty($_POST['txt_duration'.$number])) {
        $duration_save = $_POST['txt_duration'.$number];
    }
    $price_save = "";
    if (!empty($_POST['txt_price'.$number])) {
        $price_save = $_POST['txt_price'.$number];
    }
    $movie_save = "";
    if (!empty($_POST['ddl_movie'.$number])) {
        $movie_save = $_POST['ddl_movie'.$number];
    }
    $cinema_save = "";
    if (!empty($_POST['ddl_cinema'.$number])) {
        $cinema_save = $_POST['ddl_cinema'.$number];
    }
    
    $error = api_tickets::validate_ticket_fields($name_save, $show_date_save, 
                $time_start_save, $duration_save, $movie_save, $cinema_save, $price_save);
    if (empty($error)) {
        
        $movie_id = api_tickets::get_movie_id_from_title($movie_save);
        $cinema_id = api_tickets::get_cinema_id_from_name($cinema_save);
        
        if (api_tickets::save_ticket($id_save, $name_save, $show_date_save, 
                $time_start_save, $duration_save, $movie_id, $cinema_id, $price_save)) {
            $success_info = "";
            $success_info .= '<div class="ui success message">';
            $success_info .= '<i class="close icon"></i>';
            $success_info .= '<div class="header">';
            $success_info .= "Added &lt;".$name_save."&gt; ticket successfully.";
            $success_info .= "</div></div>";
            return $success_info;
        }
    }
    
    $error_show = '';
    $error_show .= '<div class="ui error message">';
    $error_show .= '    <i class="close icon"></i>';
    $error_show .= '    <div class="header">';
    $error_show .= '        add &lt;'.$name_save.'&gt; ticket failed with these wrongs.';
    $error_show .= '    </div>';
    $error_show .= '    <ul class="list">';
    $error_show .= $error;
    $error_show .= '    </ul>';
    $error_show .= '</div>';
    return $error_show;
}







$id = 0;
$name = "";
$show_date = "";
$time_start = "";
$duration = "";
$price = "";
$movie = api_tickets::get_default_movie();
$cinema = api_tickets::get_default_cinema();
$action = "Add";

if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
    if (!empty($_GET['Id'])) {
        $id = $_GET['Id'];
    }
    $edit_item = api_tickets::get_ticket_by_id($id);
    $id = $edit_item['id'];
    $name = $edit_item['name'];
    
    $show_date = $edit_item['show_date'];
    $date = date_create($show_date);
    $show_date = date_format($date, "d/m/Y");
    
    $time_start = $edit_item['time_start'];
    $date = date_create($time_start);
    $time_start = date_format($date, "H:i");
                                    
    $duration = $edit_item['duration'];
    $price = $edit_item['price'];
    $movie = $edit_item['movie_title'];
    $cinema = $edit_item['cinema_name'];
    $action = "Update";
} else if (!empty($_POST)) {
    $id_save = "";
    if (!empty($_POST['hidd_id'])) {
        $id_save = $_POST['hidd_id'];
    }
    $name_save = "";
    if (!empty($_POST['txt_name'])) {
        $name_save = $_POST['txt_name'];
    }
    $show_date_save = "";
    if (!empty($_POST['dtp_show_date'])) {
        $show_date_save = $_POST['dtp_show_date'];
    }
    $time_start_save = "";
    if (!empty($_POST['dtp_time_start'])) {
        $time_start_save = $_POST['dtp_time_start'];
    }
    $duration_save = "";
    if (!empty($_POST['txt_duration'])) {
        $duration_save = $_POST['txt_duration'];
    }
    $price_save = "";
    if (!empty($_POST['txt_price'])) {
        $price_save = $_POST['txt_price'];
    }
    $movie_save = "";
    if (!empty($_POST['ddl_movie'])) {
        $movie_save = $_POST['ddl_movie'];
    }
    $cinema_save = "";
    if (!empty($_POST['ddl_cinema'])) {
        $cinema_save = $_POST['ddl_cinema'];
    }
    if (!empty($_POST['Action']) && $_POST['Action'] === "Update") {
        $error_show = api_tickets::validate_ticket_fields($name_save, $show_date_save, 
                $time_start_save, $duration_save, $movie_save, $cinema_save, $price_save);
        if (empty($error_show)) {
            $movie_id = api_tickets::get_movie_id_from_title($movie_save);
            $cinema_id = api_tickets::get_cinema_id_from_name($cinema_save);
            if (api_tickets::save_ticket($id_save, $name_save, $show_date_save, 
                $time_start_save, $duration_save, $movie_id, $cinema_id, $price_save)) {
                
                $success_info = "Update &lt;".$name_save."&gt; ticket successfully.";
            }
        } else {
            $id = $id_save;
            $name = $name_save;
            $show_date = $show_date_save;
            $time_start = $time_start_save;
            $duration = $duration_save;
            $price = $price_save;
            $movie = $movie_save;
            $cinema = $cinema_save;
            $action = $_POST['Action'];
        }
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
        $id_delete = 0;
        if (!empty($_POST['hidd_delete_id'])) {
            $id_delete = $_POST['hidd_delete_id'];
        }
        api_tickets::delete_ticket($id_delete);
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Add All") {
        $number = 0;
        $info = "";
        while (!empty($_POST['index'.$number]) && $_POST['index'.$number] == 1) {
            $info .= add($number);
            $number++;
        }
    }
}


?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="ui page grid">
            <div class="column ts-wrapper">
                <?php
                require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
                ?>
                <div class="ts-inner-wrapper">
                    <h1>Tickets</h1>
                    <?php if (!empty($error_show)) { ?>
                                    <div class="ui error message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            Please correct these wrongs.
                                        </div>
                                        <ul class="list">
                                            <?php echo $error_show; ?>
                                        </ul>
                                    </div>
                                <?php } else if (!empty($success_info)) {
                                    ?>
                                <div class="ui success message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            <?php echo $success_info; ?>
                                        </div>
                                </div>
                                    <?php
                                } else if (!empty($info)) {
                                    echo $info;
                                }
                                ?>
                    <div class="hide" id="add-edit-popup">
                        <form action="tickets.php" method="POST" id="form_delete" >
                            <input type="hidden" name="hidd_delete_id" value="0" />
                            <input type="hidden" name="Action" value="Delete" />
                        </form>
                        <form name="form_data" id="form_data" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
                        <div class="ui form segment form-background">
                            
                            <input type="hidden" name="hidd_id" value="<?php echo $id; ?>" />
                            
                            <div class="field">
                                <label for="Name">Name <span class="required">*</span></label>
                                <input name="txt_name" type="text" value="<?php echo $name; ?>" placeholder="e.g: ticket 1" />
                            </div>
                            
                            <div class="field">
                                <label for="Status">Movie <span class="required">*</span></label>
                                <div class="ui selection dropdown">
                                    <input type="hidden" name="ddl_movie" value="<?php echo $movie; ?>">
                                    <div class="text"><?php echo $movie; ?></div>
                                    <i class="dropdown icon"></i>
                                    <div class="menu ui transition hidden">
                                      <?php 
                                        $list = api_tickets::get_all_movies();
                                        if (!empty($list) && count($list) > 0) {
                                            foreach ($list as $item) {?>
                                                <div class="item"><?php echo $item['title']; ?></div>
                                      <?php
                                            } 
                                        }?>
                                    </div>
                                  </div>
                            </div>
                            
                            <div class="field">
                                <label for="Status">Cinema <span class="required">*</span></label>
                                <div class="ui selection dropdown">
                                    <input type="hidden" name="ddl_cinema" value="<?php echo $cinema; ?>">
                                    <div class="text"><?php echo $cinema; ?></div>
                                    <i class="dropdown icon"></i>
                                    <div class="menu ui transition hidden">
                                      <?php 
                                        $list = api_tickets::get_all_cinemas();
                                        if (!empty($list) && count($list) > 0) {
                                            foreach ($list as $item) {?>
                                                <div class="item"><?php echo $item['name']; ?></div>
                                      <?php
                                            } 
                                        }?>
                                    </div>
                                  </div>
                            </div>
                            
                            <div class="field">
                                <script type="text/javascript">
                                    $(document).ready(function() {
                                        jQuery('#dtp_show_date').datetimepicker({
                                            timepicker:false,
                                            format:'d/m/Y'
                                           });
                                    });
                                </script>
                                <label for="Status">Date <span class="required">*</span></label>
                                <input name="dtp_show_date" value="<?php echo $show_date; ?>" type="text" id="dtp_show_date" placeholder="e.g: 25/05/2014" style="width:200px;">
                            </div>
                            
                            <div class="field">
                                <script type="text/javascript">
                                    $(document).ready(function() {
                                        jQuery('#dtp_time_start').datetimepicker({
                                            datepicker:false,
//                                            allowTimes:[
//                                             '12:00', '13:00', '15:00', 
//                                             '17:00', '17:05', '17:20', '19:00', '20:00'
//                                            ],
                                            format:'H:i'
                                           });
                                    });
                                </script>
                                <label for="Status">Time Start <span class="required">*</span></label>
                                <input name="dtp_time_start" value="<?php echo $time_start; ?>" type="text" id="dtp_time_start" placeholder="e.g: 21:30" style="width:200px;">
                            </div>
                            
                            <div class="field">
                                <label for="Name">Duration (min) <span class="required">*</span></label>
                                <input name="txt_duration" value="<?php echo $duration; ?>" type="text" placeholder="e.g: 60 (min)" />
                            </div>

                            <div class="field">
                                <label for="Name">Price (USD) <span class="required">*</span></label>
                                <input name="txt_price" value="<?php echo $price; ?>" type="text" placeholder="e.g: 50 (USD)" />
                            </div>
                            
                            <input type="hidden" name="Action" value="add">
                            <div class="ui buttons">
                                <a href="?"><div class="ui  button">Cancel</div></a>
                                <div class="or"></div>
                                <div id="action-button" class="ui orange button" onclick="submit_data();"><?php echo $action; ?></div>
                            </div>
                        </div>
                        </form>
                    </div>
                    
                    <div id="adds">
                        
                    </div>
                    
                    
                    <h2>List</h2>
                    
                    <table class="ui table segment">
                        <thead>
                          <tr><th>Name</th>
                          <th>Movie</th>
                          <th>Cinema</th>
                          <th>Date</th>
                          <th>Time Start</th>
                          <th>Duration (min)</th>
                          <th>Price (USD)</th>
                          <th>Available Seats</th>
                          <th>Total Seats</th>
                          <th>Out of Date</th>
                          <th>Actions</th>
                        </tr></thead>
                        <tbody>
                            <?php 
                            $list = api_tickets::get_all_live_first_dead_later();
                            if (empty($list) || count($list) == 0) {
                                echo "<tr><td>No item found.</td></tr>";
                            } else {
                                $current_page = 1;
                                if (!empty($_GET['p'])) {
                                        $current_page = $_GET['p'];
                                }
                                
                                $list_total = $list;
                                $page_size = 10;
                                $lib_pager = new lib_pager($list_total, $page_size);
                                $total_page = $lib_pager->get_total_page();
                                $current_page_list = $lib_pager->get_current_page_list($current_page);
                                foreach ($current_page_list as $item) {?>
                            
                            <tr>
                            <td><?php echo $item['name']; ?></td>
                            <td><?php echo $item['movie_title']; ?></td>
                            <td><?php echo $item['cinema_name']; ?></td>
                            <td>
                                <?php 
                                $date = date_create($item["show_date"]);
                                echo date_format($date, "d/m/Y");
                                ?>
                            </td>
                            <td>
                                <?php 
                                $date = date_create($item["time_start"]);
                                echo date_format($date, "H:i");
                                ?>
                            </td>
                            <td><?php echo $item['duration']; ?></td>
                            <td><?php echo $item['price']; ?></td>
                            <td><?php echo $item['available_seats']; ?></td>
                            <td><?php echo $item['total_seats']; ?></td>
                            <td><?php echo $item['out_of_date']; ?></td>
                            <td>
                                <a href="?Action=Edit&Id=<?php echo $item['id']; ?>">
                                <i class="pencil icon"></i>
                                </a>
                                <i class="remove icon" onclick="confirmDelete('<?php echo $item['id']; ?>')"></i>
                            </td>
                            </tr>
                            <?php
                                }
                            }?>
                        </tbody>
                        <tfoot>
                          <tr><th colspan="11">
                            <label>Entry number </label>
                            <div class="ui input">
                                <input id="txt_entry_number" name="txt_entry_number" type="text" width="3em" placeholder="1">
                            </div>
                            <div class="ui orange labeled icon button" onclick="show_add_popup('add_popup_tickets');" ><i class="ticket icon"></i> Add Tickets</div>
                            <table class="paging-footer">
                                    <tbody>
                                            <tr>
                                                    <?php 
                                                    if (!empty($total_page) && count($total_page) > 0) {
                                                        for ($page = 1; $page <= $total_page; $page++) { ?>
                                                        <td>
                                                                <span>
                                                                        <a href="?p=<?php echo $page; ?>" <?php if ($current_page != $page) echo 'style="color:Black;"'; ?>><?php echo $page; ?></a>
                                                                </span>
                                                        </td>
                                                    <?php }
                                                    }?>
                                            </tr>
                                    </tbody>
                            </table>
                          </th>
                        </tr></tfoot>
                      </table>
                    
                    <script type="text/javascript">
                        <?php 
                        if (!empty($_GET['Action']) && $_GET['Action'] === "Edit") {
                            echo "show_popup();";
                        }
                        ?>
                    </script>
                </div>
                <?php
                require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
                ?>
            </div>
        </div>
    </body>
</html>